package com.itheima.reggie.controller.backend;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.constant.CommonConstant;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.util.IpUtil;
import com.itheima.reggie.util.LoginUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/backends/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private LoginUserUtil loginUserUtil;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public Result<Employee> login(@RequestBody Employee employee){

        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername()).eq(Employee::getPassword,password);
        Employee emp = employeeService.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if(emp == null){
            return Result.error("登录失败");
        }

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if(emp.getStatus() == 0){
            return Result.error("账号已禁用");
        }


        //6、登录成功，将员工id存入Session并返回登录成功结果
        emp.setPassword(null);
        loginUserUtil.setBackendUser(emp.getId(),30L);
        return Result.success(emp);
    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(){
        loginUserUtil.removeBackendUser(null);
        return Result.success("退出成功");
    }

    @PostMapping
    public Result<Employee> add(@RequestBody Employee employee){

        if(employee == null){
            return Result.error("参数传递未空");
        }
        employeeService.add(employee);
        employee.setPassword(null);
        return Result.success(employee);
    }

    /**
     * 员工信息查询
     * @param page
     * @return
     */
    @GetMapping("/page")
    public Result<JSONObject> page(Integer page, Integer pageSize, String name){
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(Employee.class,info->!info.getColumn().equals(CommonConstant.MYSQL_EMPLOYEE_PASSWORD));
        if(StringUtils.isNotEmpty(name)) {
            queryWrapper.like(CommonConstant.MYSQL_EMPLOYEE_USERNAME,name).or().like(CommonConstant.MYSQL_EMPLOYEE_NAME,name);
        }
        IPage<Employee> pages=new Page<>((page-1)*pageSize,pageSize);
        employeeService.page(pages,queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("records",pages.getRecords());
        jsonObject.put("total",pages.getTotal());

        return Result.success(jsonObject);
    }

    @PutMapping
    public Result<String> edit(@RequestBody Employee employee){
        System.out.println("edit--------------------------------------");
        if(employee == null){
            return Result.error("参数传递未空");
        }

        if(employee.getId() == null){
            return Result.error("员工编号为空");
        }
        employeeService.edit(employee);

        return Result.success("修改成功");
    }

    /**
     * 员工信息根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable String id){

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,Long.parseLong(id));
        Employee one = employeeService.getOne(queryWrapper);


        return Result.success(one);
    }

}
